Exercises
Art Store, Revisited
Let's get some practice! We'll update the “art store” example we saw earlier in this module to use useReducer.
Acceptance Criteria:
- The
cartItemsstate should use theuseReducerhook. - You should use the conventional switch/case structure discussed in the previous lesson within your reducer.
Code Playground
Solution:
Counter 2.0, One Last Time
I promise, this is the last time we'll hack on this counter example in this course!
Update the code below so that it uses the useReducer hook for the count state.
Acceptance Criteria:
- The
countstate should use theuseReducerhook. - You should use the conventional switch/case structure discussed in the previous lesson within your reducer.
- Each button should get its own action type.
Code Playground
Solution:
Gradient Generator, Revisited
Let's tackle one more example: the “Gradient Generator” tool from Module 2.
This one is a bit trickier, because there are two state variables. Your goal is to merge them into a single reducer.
Acceptance Criteria:
- All state should be managed with the
useReducerhook. - In order for this to work, the state will need to be stored as an object that contains both the
colorsarray and thenumOfVisibleColorsnumber.
This is a difficult challenge — I haven't yet shown you exactly how to solve these sorts of problems. Unless you have previous experience with Redux, you might not be able to solve it, and that's OK! The goal here is to spend 15 minutes giving it your best shot.
I'll explain everything in the solution video below. ❤️
Code Playground
Solution: